(function($, doc) {
	$.plusReady(function() {
		mui.init();
		var user = app.getState();
		var uid = user.user_id;
		var currentmoney = 10;
		$('#selecttype').on('tap', 'span', function() {
			//console.log(this.id);
			doc.querySelector('.click').classList.remove('click');
			this.classList.add('click');
			currentmoney = this.innerHTML * 1;
		});

		function renderbank(data) {
			doc.getElementById('bank_account').innerHTML = data.bank_account;
			doc.getElementById('bank_name').innerHTML = data.bank_name;
			doc.getElementById('bank_addr').innerHTML = data.bank_addr;
			doc.getElementById('bank_card').innerHTML = data.bank_card;
			doc.getElementById('addcard_after').style.display = '';
		}

		function opencard() {
			$.openWindow({
				url: 'add_card.html',
				id: 'add_card'
			});
		}
		$.post(apiurl + 'Bank/getDefBank', {
			uid: uid
		}, function(res) {
			//console.log(JSON.stringify(res));
			if(res.status && res.status > 0) {
				var bank = res.data;
				renderbank(bank);
				user.bankid = bank.id;
				user.bank_account = bank.bank_account;
				user.bank_card = bank.bank_card;
				user.bank_name = bank.bank_name;
				user.bank_addr = bank.bank_addr;
				app.setState(user);
				doc.getElementById('mycard').addEventListener('tap', opencard);
			} else {
				doc.getElementById('addcard').style.display = '';
				//doc.getElementById('descarea').style.display = 'none';
				doc.getElementById('subarea').style.display = 'none';
				doc.getElementById('add_card').addEventListener('tap', opencard);
			}
		});
		window.addEventListener('refreshbank', function(e) {
			var data = e.detail;
			doc.getElementById('addcard').style.display = 'none';
			//doc.getElementById('descarea').style.display = '';
			doc.getElementById('subarea').style.display = '';
			renderbank(data);

		});
		doc.getElementById('create').addEventListener('tap', function() {
			var user = app.getState();
			if(!user.bankid) {
				$.alert("请绑定银行卡！");
				return;
			}
			var passwd = doc.getElementById('passwd').value;
			if(!passwd) {
				$.alert("交易密码不能为空！");
				return;
			}
			var wallet = ethscan.getWallet('eth');
			if(!wallet || !wallet.address) {
				$.alert("请创建钱包！");
				return;
			}
			var user = app.getState();
			var postdata = {
				uid: uid,
				sale_hash: '',
				user_address : wallet.address,
				trade_pwd: passwd,
				trade_money: currentmoney,
				money_type: 'gatcsale'/*defaultCoinAddr*/,
				bank_id: user.bankid
				//remark : doc.getElementById('remark').innerHTML
			};
			$.post(apiurl + "order/tradeSale", postdata, function(res) {
				if(res && res.status && res.data) {
					$.alert(res.msg);
//					ethscan.payToSystem(currentmoney, function(hash, addr) {
//						if(!hash) {
//							$.alert("支付失败！");
//							return;
//						}
//						//var hash = '547394539';
//						var postdata = {
//							uid : uid,
//							id : res.data,
//							sale_hash : hash
//						};
//
//						$.post(apiurl + "order/upHash", postdata, function(res) {
//							console.log(JSON.stringify(res));
//							if(res.status && res.status > 0) {
//								$.alert('卖出成功！');
//								$.openWindow({
//									url: 'order_list.html',
//									id: 'order_list'
//								});
//							} else {
//								$.toast('创建订单失败');
//							}
//						});
//					});
				} else {
					$.alert(res.msg);
				}
			});

		});

	});
}(mui, document));